package com.smart.community.region.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import com.smart.community.commons.result.Result;
import com.smart.community.region.dto.ResidentHouseholdAddDTO;
import com.smart.community.region.service.IResidentHouseholdService;
import com.smart.community.region.vo.PageResult;
import com.smart.community.region.vo.ResidentHouseholdVO;
import com.smart.community.region.dto.ResidentHouseholdUpdateDTO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 住户房户关联Controller
 * 
 * @author Wu.Liang
 * @since 2025-07-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/region/resident-households")
@Tag(name = "住户房户关联管理", description = "住户房户关联管理相关接口")
public class ResidentHouseholdController {

	@Autowired
	private IResidentHouseholdService residentHouseholdService;

	/**
	 * 查询房户住户列表
	 * 
	 * @throws Exception
	 */
	@GetMapping("/household/{householdId}")
	@Operation(summary = "查询房户住户列表", description = "分页查询指定房户的住户列表")
	public Result<PageResult<ResidentHouseholdVO>> getResidentList(@PathVariable String householdId,
			@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size)
			throws Exception {
		// 验证householdId参数
		if (householdId == null || "undefined".equals(householdId) || "null".equals(householdId)) {
			return Result.error("房户ID不能为空");
		}
		
		try {
			Long householdIdLong = Long.valueOf(householdId);
			PageResult<ResidentHouseholdVO> result = residentHouseholdService.getResidentList(householdIdLong, current, size);
			return Result.success(result);
		} catch (NumberFormatException e) {
			log.error("房户ID格式错误: {}", householdId, e);
			return Result.error("房户ID格式错误");
		}
	}

	@ApiLog(logTitle = "添加住户到房户", logType = 2, moduleName = "区域管理", operationType = ApiLogOperationType.ADD)
	@PostMapping("/household/{householdId}/add")
	@Operation(summary = "添加住户到房户", description = "将住户添加到指定房户")
	public Result<Void> addResidentToHousehold(@PathVariable String householdId,
			@Valid @RequestBody ResidentHouseholdAddDTO dto) throws Exception {
		// 验证householdId参数
		if (householdId == null || "undefined".equals(householdId) || "null".equals(householdId)) {
			return Result.error("房户ID不能为空");
		}
		
		try {
			Long householdIdLong = Long.valueOf(householdId);
			residentHouseholdService.addResidentToHousehold(householdIdLong, dto);
			return Result.success();
		} catch (NumberFormatException e) {
			log.error("房户ID格式错误: {}", householdId, e);
			return Result.error("房户ID格式错误");
		}
	}

	@ApiLog(logTitle = "移除住户从房户", logType = 2, moduleName = "区域管理", operationType = ApiLogOperationType.DELETE)
	@DeleteMapping("/household/{householdId}/resident/{residentId}")
	@Operation(summary = "移除住户从房户", description = "从房户中移除指定住户")
	public Result<Void> removeResidentFromHousehold(@PathVariable String householdId, @PathVariable String residentId)
			throws Exception {
		// 验证householdId参数
		if (householdId == null || "undefined".equals(householdId) || "null".equals(householdId)) {
			return Result.error("房户ID不能为空");
		}
		
		// 验证residentId参数
		if (residentId == null || "undefined".equals(residentId) || "null".equals(residentId)) {
			return Result.error("住户ID不能为空");
		}
		
		try {
			Long householdIdLong = Long.valueOf(householdId);
			Long residentIdLong = Long.valueOf(residentId);
			residentHouseholdService.removeResidentFromHousehold(householdIdLong, residentIdLong);
			return Result.success();
		} catch (NumberFormatException e) {
			log.error("ID格式错误: householdId={}, residentId={}", householdId, residentId, e);
			return Result.error("ID格式错误");
		}
	}

	@ApiLog(logTitle = "更新住户房户关联信息", logType = 2, moduleName = "区域管理", operationType = ApiLogOperationType.UPDATE)
    @PutMapping("/{id}")
    @Operation(summary = "更新住户房户关联信息", description = "更新住户在房户中的关联信息")
    public Result<Void> updateResidentHousehold(@PathVariable String id, @Valid @RequestBody ResidentHouseholdUpdateDTO dto)
            throws Exception {
		// 验证id参数
		if (id == null || "undefined".equals(id) || "null".equals(id)) {
			return Result.error("关联ID不能为空");
		}
		
		try {
            Long idLong = Long.valueOf(id);
            residentHouseholdService.updateResidentHousehold(idLong, dto);
			return Result.success();
		} catch (NumberFormatException e) {
			log.error("关联ID格式错误: {}", id, e);
			return Result.error("关联ID格式错误");
		}
	}

	/**
	 * 查询住户的房户列表
	 * 
	 * @throws Exception
	 */
	@GetMapping("/resident/{residentId}")
	@Operation(summary = "查询住户的房户列表", description = "查询指定住户关联的所有房户")
	public Result<PageResult<ResidentHouseholdVO>> getHouseholdList(@PathVariable String residentId,
			@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size)
			throws Exception {
		// 验证residentId参数
		if (residentId == null || "undefined".equals(residentId) || "null".equals(residentId)) {
			return Result.error("住户ID不能为空");
		}
		
		try {
			Long residentIdLong = Long.valueOf(residentId);
			PageResult<ResidentHouseholdVO> result = residentHouseholdService.getHouseholdList(residentIdLong, current, size);
			return Result.success(result);
		} catch (NumberFormatException e) {
			log.error("住户ID格式错误: {}", residentId, e);
			return Result.error("住户ID格式错误");
		}
	}
}